package com.summer.socket;

import java.net.InetSocketAddress;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

/**
 * Netty 实现
 * 
 * @author Administrator
 *
 */
public class SimpleCase3 {

	public static void main(String[] args) {
		// 服务启动引导程序类
		ServerBootstrap bootstrap = new ServerBootstrap();
		
		EventLoopGroup parentGroup = new NioEventLoopGroup();
		EventLoopGroup childGroup  = new NioEventLoopGroup();
		// 设置NioSocket工厂
		bootstrap.group(parentGroup, childGroup)
			.channel(NioServerSocketChannel.class)
			.localAddress(new InetSocketAddress(9999))
			.childHandler(new ChannelInitializer<Channel>() {
				@Override
				protected void initChannel(Channel ch) throws Exception {
					ChannelPipeline pipeline = ch.pipeline();
					pipeline.addLast("decoder", new StringDecoder());
					pipeline.addLast("encoder", new StringEncoder());
					pipeline.addLast("helloHandler",new HelloHandler());
				}
			});
		
		bootstrap.bind();
		System.out.println("开启服务！！！");
	}
	
	public static class HelloHandler extends SimpleChannelInboundHandler<String> {

		/**
		 * 向EventLoop中注册了通道
		 * 可在此做IP过滤黑名单
		 */
		@Override
		public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
			System.out.println("channelRegistered");
			ctx.channel().close();
			super.channelRegistered(ctx);
		}
		
		/**
		 * 通道被激活了，处于激活状态
		 */
		@Override
		public void channelActive(ChannelHandlerContext ctx) throws Exception {
			System.out.println("channelActive");
			super.channelActive(ctx);
		}
		
		/**
		 * 读取通道数据
		 */
		@Override
		public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
			System.out.println("channelRead::"+msg);
			super.channelRead(ctx, msg);
			
			ctx.channel().writeAndFlush(msg);
//			try {
//				int a = 1/0;
//			} catch (Exception e) {
//				// TODO: handle exception
//			}
		}
		
		@Override
		protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
			System.out.println("channelRead0::"+msg);
			
		}

		/**
		 * 捕获异常
		 */
		@Override
		public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
			System.out.println("exceptionCaught");
			super.exceptionCaught(ctx, cause);
		}
		
		/**
		 * 读取后出发完成
		 */
		@Override
		public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
			System.out.println("channelReadComplete");
			super.channelReadComplete(ctx);
		}
		
		/**
		 * 通道已注册，现在停用并且达到生命结束
		 */
		@Override
		public void channelInactive(ChannelHandlerContext ctx) throws Exception {
			System.out.println("channelInactive");
			super.channelInactive(ctx);
		}
		
		/**
		 * 从EventLoop中取消通道注册
		 */
		@Override
		public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
			System.out.println("channelUnregistered");
			super.channelUnregistered(ctx);
		}
		
	}
	
	
	
}
